package com.huamao.niceman.scrollviewpager.utils;


import android.util.Log;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by LuanXianSheng on 2017/6/13.
 * 双重检查锁定
 */
public class RetrofitSingleton {
    private static  Retrofit mRetrofit = null;
    private RetrofitSingleton() {
    }

    public static Retrofit newInstance() {
        if (mRetrofit == null){
            synchronized (RetrofitSingleton.class){
                mRetrofit = new Retrofit.Builder().baseUrl(UrlUtils.URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .client(getOkHttpClient())//使用自己创建的OkHttp
                        .build();
            }
        }
        return mRetrofit ;
    }
    private static OkHttpClient getOkHttpClient() {
        //日志显示级别
        HttpLoggingInterceptor.Level level= HttpLoggingInterceptor.Level.BODY;
        //新建log拦截器
        HttpLoggingInterceptor loggingInterceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.d("AAA","OkHttp====Message:"+message);
            }
        });
        loggingInterceptor.setLevel(level);
        //定制OkHttp
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient
                .Builder();
        //OkHttp进行添加拦截器loggingInterceptor
        httpClientBuilder.addInterceptor(loggingInterceptor);//失败重连
        return httpClientBuilder .retryOnConnectionFailure(true).build();
    }
}
